/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.domain.SubjectsQuests;
import me.zhengjie.service.SubjectsQuestsService;
import me.zhengjie.service.dto.SubjectsQuestsQueryCriteria;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import me.zhengjie.utils.PageResult;
import me.zhengjie.service.dto.SubjectsQuestsDto;

/**
* @website https://eladmin.vip
* @author wuhj
* @date 2023-09-16
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "科目明细管理")
@RequestMapping("/api/subjectsQuests")
public class SubjectsQuestsController {

    private final SubjectsQuestsService subjectsQuestsService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('subjectsQuests:list')")
    public void exportSubjectsQuests(HttpServletResponse response, SubjectsQuestsQueryCriteria criteria) throws IOException {
        subjectsQuestsService.download(subjectsQuestsService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询科目明细")
    @ApiOperation("查询科目明细")
    @PreAuthorize("@el.check('subjectsQuests:list')")
    public ResponseEntity<PageResult<SubjectsQuestsDto>> querySubjectsQuests(SubjectsQuestsQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(subjectsQuestsService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增科目明细")
    @ApiOperation("新增科目明细")
    @PreAuthorize("@el.check('subjectsQuests:add')")
    public ResponseEntity<Object> createSubjectsQuests(@Validated @RequestBody SubjectsQuests resources){
        subjectsQuestsService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改科目明细")
    @ApiOperation("修改科目明细")
    @PreAuthorize("@el.check('subjectsQuests:edit')")
    public ResponseEntity<Object> updateSubjectsQuests(@Validated @RequestBody SubjectsQuests resources){
        subjectsQuestsService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除科目明细")
    @ApiOperation("删除科目明细")
    @PreAuthorize("@el.check('subjectsQuests:del')")
    public ResponseEntity<Object> deleteSubjectsQuests(@RequestBody Long[] ids) {
        subjectsQuestsService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/getTerminalQuests")
    @Log("终端绑定考题")
    @ApiOperation("查询终端绑定考题")
    @AnonymousAccess
    public ResponseEntity<List<SubjectsQuestsDto>> queryTerminalQuests(@RequestBody SubjectsQuestsQueryCriteria criteria){
        return new ResponseEntity<>(subjectsQuestsService.queryAll(criteria),HttpStatus.OK);
    }
}